package com.ailk.cuc.autotestbg.common.utils;

import java.io.File;
import java.util.ArrayList;

import javax.mail.internet.MimeUtility;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;

import com.ailk.cuc.autotestbg.common.constants.StaticParam;

public class MailUtils {

	public static void mailSend(ArrayList<String> attachList,String subject) {
		Log logger = LogFactory.getLog(MailUtils.class.getName());
		
		try {
			MultiPartEmail email = new MultiPartEmail();
			email.setFrom(StaticParam.fromMail);
			email.setHostName(StaticParam.smtp);
			email.setAuthentication(StaticParam.mailUser,StaticParam.mailPasswd);
			if (StaticParam.ssl.equals("Y")) {
				email.setSSL(true);
				email.setSslSmtpPort(StaticParam.sslPort);
			}
			
			for(int i=0;i<StaticParam.addMail.size();i++) {
				email.addTo(StaticParam.addMail.get(i));
			}
			
			for(int i=0;i<attachList.size();i++) {
				EmailAttachment attach = new EmailAttachment();
				attach.setPath(attachList.get(i));
				File file = new File(attachList.get(i));
				attach.setName(MimeUtility.encodeWord(file.getName(), "GB2312", "B").trim());
				attach.setDisposition(EmailAttachment.ATTACHMENT);
				attach.setDescription("测试报告");
				email.attach(attach);
			}
			
			email.setSubject("AIBGAT自动测试平台" + subject + "测试报告");
			email.setCharset("GB2312");
			email.setMsg("测试报告请见附件!");
			email.send();
		}
		catch (Exception e) {
			logger.error("发送邮件时发生异常:" + e.getMessage());
			e.printStackTrace();
		}
	}
	
	public static boolean inspectResultSend(String htmlStr,String subject,String htmlFile,String mailList) {
		Log logger = LogFactory.getLog(MailUtils.class.getName());
		
		try {
			HtmlEmail email = new HtmlEmail();
			email.setFrom(StaticParam.fromMail,"总部项目维护组","GB2312");
			email.setHostName(StaticParam.smtp);
			email.setAuthentication(StaticParam.mailUser,StaticParam.mailPasswd);
			if (StaticParam.ssl.equals("Y")) {
				email.setSSL(true);
				email.setSslSmtpPort(StaticParam.sslPort);
			}
			
			String[] toList = mailList.split(";");
			
			for(int i=0;i<toList.length;i++) {
				email.addTo(toList[i]);
			}
			
			email.setSubject(subject);
			email.setCharset("GB2312");
			email.setHtmlMsg(htmlStr);
			email.setTextMsg("如果您看到这句话,说明您的邮件客户端不支持查看Html邮件,请您点击打开附件中的html文件查看,谢谢!");
			
			EmailAttachment attach = new EmailAttachment();
			attach.setPath(htmlFile);
			File file = new File(htmlFile);
			attach.setName(MimeUtility.encodeWord(file.getName(), "GB2312", "B").trim());
			attach.setDisposition(EmailAttachment.ATTACHMENT);
			attach.setDescription(subject);
			email.attach(attach);
			
			email.send();
			return true;
		}
		catch(Exception e) {
			logger.error("自动巡检结果报告邮件发送失败:" + e.getMessage());
			e.printStackTrace();
			return false;
		}
	}
}